package com.shujia.six;

import org.junit.Test;

import java.io.*;
import java.util.Scanner;

public class PrintStreamDemo {

    /*
    从键盘输入字符串，要求将读取到的整行字符串转成大写输出。然后继续进行输入操作，
    直至当输入“e”或者“exit”时，退出程序，要求使用缓冲流包装标准输入流实现。
     */
    @Test
    public void test5() throws IOException {
        // System.in 字节流, 键盘录入
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String str;
        while ((str = br.readLine()) != null) {
            if ("e".equals(str) || "exit".equals(str)) {
                break;
            }
            System.out.println(str.toUpperCase());
        }

        br.close();
        isr.close();
    }

    @Test
    public void test4() throws IOException {
//        Scanner sc = new Scanner(System.in);
        Scanner sc = new Scanner(new FileInputStream("file\\a.txt"));
        String str;
        while (sc.hasNext()) {
            System.out.println(sc.next());
        }
    }

    @Test
    public void test3() throws IOException {
        PrintWriter ps = new PrintWriter(new FileOutputStream("file\\d.txt"));
        ps.print(1);
        ps.print(2);

        ps.close();
    }

    @Test
    public void test2() throws IOException {
        PrintStream ps = new PrintStream(new FileOutputStream("file\\c.txt"));
        System.setOut(ps);

        System.out.println("nihao");
        System.out.println("haha");

    }

    @Test
    public void test1() throws IOException {
        PrintStream ps = new PrintStream(new FileOutputStream("file\\c.txt"));
        ps.println(1);
        ps.println(true);
        ps.println(2222222L);
        ps.flush();
        ps.close();

    }
}
